<div ng-controller="Purchasing_AddPurchasingC">
<div class="modal fade" id="dlg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">{{typeName}}选择</h4>
                </div>
                <div class="modal-body">
                    <!--<input class="selectFind form-control" type="text" ng-model="searchTxt" placeholder="查找" />-->
                    <div class="input-group ">
                        <input class="selectFind form-control" type="text" ng-model="Name" placeholder="查找">
                        <span class="glyphicon glyphicon-remove input-group-addon" ng-click="empty()"></span>
                    </div>
                    <div class="selectShow" style=" width: 100%; height: 200px;border:1px solid #CCCCCC;overflow:auto;">
                        <div style="cursor:pointer;" class="form-control" ng-click="selectEmp(x)" ng-repeat=" x in cangku | filter:{ Name : Name} ">{{x.Name}}</div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>
            </div>
        </div>
    </div>
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">采购单申请</h1>
    </div>
    <div class="wrapper-md">
        <div class="panel panel-default">
            <div class="table-responsive" ng-show="isshow">
                <form name="form" class="form-validation">
                    <div class="panel panel-default">
                        <div class="panel-body">
                            <div class="form-group">
                                <label>供应商--{{typeName}}</label>
                                <button required="required" class="form-control text-left" type="text" data-toggle="modal" data-target="#dlg" ng-model="orderdata.Provider"  ng-change="change1()">
                                    {{orderdata.Provider.Name}}
                                </button>
                            </div>
                            <div class="form-group">
                                <label>收货仓库</label>
                                <!--<select class="form-control cangku" ng-model="orderdata.ProviderId" name="cangku">-->
                                    <!--<option ng-repeat="y in cangku" value="{{y.Id}}">{{y.Name}}</option>-->
                                <!--</select>-->
                                <select required class="form-control cangku" ng-model="orderdata.ToDepot" ng-options="value.Name for value in cangku1"></select>
                            </div>
                            <!--<div class="form-group">-->
                                <!--<label>商品打款账户</label>-->
                                <!--<select required class="form-control" ng-model="orderdata.Account">-->
                                    <!--<option value="工行洛南支行">沐威科技</option>-->
                                    <!--<option value="洛阳银行洛南支行">中伟钢物联</option>-->
                                    <!--<option value="交行西苑路支行">现金</option>-->
                                <!--</select>-->
                            <!--</div>-->
                            <div class="form-group">
                                <label for="suojia" style="margin-right: 10px;">货款是否标件</label>
                                <label class="i-checks">
                                    <input type="checkbox" id="suojia" ng-model="orderdata.IsStandard">
                                    <i></i>
                                </label>
                            </div>
                            <div class="form-group">
                                <label for="zipai" style="margin-right: 10px;" ng-click="zipai()">运费是否标件</label>
                                <label class="i-checks" ng-click="zipai()">
                                    <input type="checkbox" id="zipai" ng-model="orderdata.IsPublicDelivery">
                                    <i></i>
                                </label>
                            </div>
                            <!--<div class="form-group">-->
                                <!--<label>运费打款账户</label>-->
                                <!--<select required class="form-control" ng-model="orderdata.DeliveryAccount">-->
                                    <!--<option value="工行洛南支行">工行洛南支行</option>-->
                                    <!--<option value="洛阳银行洛南支行">洛阳银行洛南支行</option>-->
                                    <!--<option value="交行西苑路支行">交行西苑路支行</option>-->
                                    <!--<option value="伊川农商行洛龙区香山路支行">伊川农商行洛龙区香山路支行</option>-->
                                    <!--<option value="伊川农商行人民路支行">伊川农商行人民路支行</option>-->
                                    <!--<option value="李楼信用社">李楼信用社</option>-->
                                    <!--<option value="中国农业银行洛阳中泓支行">中国农业银行洛阳中泓支行</option>-->
                                    <!--<option value="中国工商银行洛阳政和支行">中国工商银行洛阳政和支行</option>-->
                                    <!--<option value="中国建设银行洛阳开元大道支行">中国建设银行洛阳开元大道支行</option>-->
                                    <!--<option value="中国银行洛阳政和路支行">中国银行洛阳政和路支行</option>-->
                                    <!--<option value="交通银行洛阳分行营业部">交通银行洛阳分行营业部</option>-->
                                    <!--<option value="河南省农村信用社洛阳经济开发区联社李屯信用社">河南省农村信用社洛阳经济开发区联社李屯信用社</option>-->
                                    <!--<option value="中信银行洛阳联盟路支行">中信银行洛阳联盟路支行</option>-->
                                    <!--<option value="兴业银行洛阳凯旋东路支行">兴业银行洛阳凯旋东路支行</option>-->
                                <!--</select>-->
                            <!--</div>-->
                            <div class="form-group">
                                <label>每吨运费</label>
                                <input required ng-model="orderdata.DeliveryPrice" type="number" min="0" class="form-control">
                            </div>
                            <!--<div class="form-group">-->
                                <!--<label class="control-label">申请日期</label>-->
                                <!--<input required type="date" class="form-control" ng-model="NowData.CreateTime">-->
                            <!--</div>-->
                            <!--<div class="form-group">-->
                                <!--<label>申请人</label>-->
                                <!--<input ng-model="orderdata.DeliveryPrice" type="text" class="form-control">-->
                            <!--</div>-->
                            <div class="form-group">
                                <label>联系人信息(非必填)</label>
                                <input ng-model="orderdata.Contact" type="text" class="form-control" placeholder="请输入联系人信息（100字内）" maxlength="100">
                            </div>
                            <div class="form-group">
                                <label>备注(非必填)</label>
                                <input ng-model="orderdata.Remarks" type="text" class="form-control" placeholder="请输入备注信息（100字内）" maxlength="100">
                            </div>
                        </div>
                        <div class="container-fluid text-center bg-light lter">
                            <!--<div class="row">-->
                                <!--<h3 style="display: inline-block; margin: 0 auto">采购商品列表</h3>-->
                            <!--</div>-->
                            <!--<div class="row text-right">-->
                                <!--<button type="button" class="btn btn-sm btn-success" ng-click="xxx()">去选商品</button>-->
                            <!--</div>-->


                            <div class="row" style="height: 40px;">
                                <div class="col-xs-6 col-md-6 text-left">
                                    <strong style="display: inline-block;line-height: 40px">采购商品列表</strong>
                                </div>
                                <a>
                                    <div ng-click="xxx()" class="col-xs-6 col-md-6 text-right" style="display: inline-block;line-height: 100%;height: 100%">
                                    <span style="display: inline-block;line-height: 40px">去选商品&nbsp;&nbsp;
                                        <i class="fa fa-chevron-right text"></i>
                                    </span>
                                    </div>
                                </a>
                            </div>
                        </div>
                        <div class="container-fluid" style="padding-top: 10px">

                            <div class="panel panel-default" ng-repeat="item in orderdata.Goodses" style="margin-bottom: 10px">
                                <div class="panel-heading">
                                    <div>钢厂：{{item.Supplier.Name}}</div>
                                </div>
                                <div class="panel-body">
                                    <div class="row">
                                        <div class="col-xs-9 col-md-9">
                                            <div>名称/材质/规格：{{item.Name}}/{{item.Standard}}/{{item.Format}}</div>
                                            <div>单价：
                                                <input ng-change="sum()" type="number" min="0" ng-model="item.Price">/t
                                            </div>
                                            <div>数量：
                                                <input ng-change="sum()" type="number" min="0" ng-model="item.Count">件
                                            </div>
                                            <div>总质量：{{item.Count}}*{{item.Weight | number:3}}t={{item.Count*item.Weight | number:3}}t</div>
                                            <!--<div>总价：{{item.Count}}*{{item.Price}}*{{item.Weight}}t={{item.Count*item.Price*item.Weight.toFixed(2)}}</div>-->
                                            <div>总价：{{item.Count}}*{{item.Price | number:2}}*{{item.Weight | number:3}}t={{(item.Count*item.Price*item.Weight) | number:2}}</div>
                                        </div>
                                        <div class="col-xs-3 col-md-3" style="text-align: center;">
                                            <button type="button" class="btn btn-sm btn-default" ng-click="Countadd(item)" style="margin: 5px">
                                                <i class="fa fa-plus text"></i>
                                            </button>
                                            <button type="button" class="btn btn-sm btn-default" ng-click="Countdeduct(item)" style="margin: 5px">
                                                <i class="fa fa-minus text"></i>
                                            </button>
                                            <button type="button" class="btn btn-sm btn-danger" ng-click="deduct(item)" style="margin: 5px">
                                                删除
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="row text-left container-fluid">
                                <span style="display: inline-block">总价：{{orderdata.Total | number:2}}</span>
                            </div>
                            <div class="row text-left container-fluid">
                                <span style="display: inline-block">预计运费价格：{{newWeight * orderdata.DeliveryPrice | number:2}}</span>
                            </div>
                            <!--<div class="row text-right">
                                <button type="button" class="btn btn-success" ng-disabled="form.$invalid" ng-click="ordersub()">提交采购单</button>
                                <button type="button" class="btn btn-default" ng-click="abolish()">返回列表页</button>
                            </div>-->
                        </div>
                         <!--<div class="row" style="text-align: center;padding: 18px 0;">-->
                                <!--<button type="button" class="btn btn-success" ng-disabled="form.$invalid" ng-click="ordersub()">提交采购单</button>-->
                                <!--<span style="padding:0 10%"></span>-->
                                <!--<button type="button" class="btn btn-default" ng-click="abolish()">返回列表页</button>-->
                            <!--</div>-->

                        <div class="row text-center" style="height: 40px;">
                            <a>
                                <div class="col-xs-6 col-md-6" style="display: inline-block;line-height: 100%;height: 100%;padding: 0px">
                                    <button type="button" style="width: 100%;height: 100%" class="btn btn-default" ng-click="abolish()">返回列表页</button>
                                </div>
                                <div class="col-xs-6 col-md-6" style="display: inline-block;line-height: 100%;height: 100%;padding: 0px">
                                    <button type="button" style="width: 100%;height: 100%" class="btn btn-success" ng-disabled="form.$invalid" ng-click="ordersub()">提交采购单</button>
                                </div>
                            </a>
                        </div>
                    </div>
                </form>
            </div>
            <div ng-show="!isshow">
                <div class="container-fluid bg-light lter">
                    <div class="row" style="height: 40px;">
                        <a>
                            <div ng-click="xxx()" class="col-xs-6 col-md-6 text-left" style="display: inline-block;line-height: 100%;height: 100%">
                                    <span style="display: inline-block;line-height: 40px">
                                        <i class="fa fa-chevron-left text"></i>&nbsp;&nbsp;返回采购单
                                    </span>
                            </div>
                        </a>
                        <div class="col-xs-6 col-md-6 text-center">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12 input-group" style="width: 250px; float: left">
                                <input type="text" class="form-control ng-pristine ng-untouched ng-valid" placeholder="按钢厂名称查找" ng-model="topname">
                                <span class="input-group-btn">
                                    <button class="btn btn-default" type="button" ng-click="genre(findName,topname,orderdata.Provider.Id)">查找</button>
                                </span>
                            </div>
                            <select style="" class="btn btn-default" ng-model="findName" ng-change="genre(findName,topname,orderdata.Provider.Id)" style=" float: left">
                                <option value="盘螺">盘螺</option>
                                <option value="线材">线材</option>
                                <option value="钢坯">钢坯</option>
                                <option value="螺纹钢">螺纹钢</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="container-fluid" style="margin-top: 10px">
                    <div class="panel panel-default" ng-repeat="item in DepotCommodity" style="margin-bottom: 10px">
                        <div class="panel-heading">
                            <div>钢厂：{{item.Supplier.Name}}</div>
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <div class="col-xs-9 col-md-9">
                                    <div>名称/材质/规格：{{item.Name}}/{{item.Standard}}/{{item.Format}}</div>
                                </div>
                                <div class="col-xs-3 col-md-3" style="text-align: center;">
                                    <button type="button" ng-show="!item.isadd" class="btn btn-sm btn-default" ng-click="add(item)">
                                        <i class="fa fa-plus text"></i>
                                        <span class="text">添加</span>
                                    </button>
                                    <button type="button" ng-show="item.isadd" class="btn btn-sm btn-default" ng-click="deduct(item)">
                                        <i class="fa fa-minus text"></i>
                                        <span class="text">取消</span>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    angular.module('app')
            //创建采购单控制器
            .controller("Purchasing_AddPurchasingC", function ($scope, $state, $http, $stateParams) {
                $scope.type = JSON.parse($stateParams.type);
                if($scope.type == 0){
                    $scope.typeName = "钢厂"
                }else if($scope.type == 1){
                    $scope.typeName = "贸易商"
                }
                $scope.findName = '盘螺';
                $scope.isshow = true;
                $scope.isadd = true;
                $scope.Weight = 0;
                $scope.newWeight = 0;
                $scope.topname='';
                //                获取列表方法
                $scope.acquire = function(type){
                    $http({
                        url: "../api/Provider?type="+type+"&page=1&rows=9999",
                        method: "GET"
                    })
                            .then(function (res) {
                                if (res.State <= 0) {
                                    alert(res.Msg);
                                } else {
                                    $scope.cangku = res.data.Data;
                                    $http({
                                            url: "../api/Provider?type=1&page=1&rows=9999",
                                            method: "GET"
                                        })
                                                .then(function (res) {
                                                    if (res.State <= 0) {
                                                        alert(res.Msg);
                                                    } else {
                                                        $scope.cangku1 = res.data.Data;
                                                        if ($scope.type!=0) {
                                                            for (var i = 0; i < $scope.cangku.length; i++) {
                                                                // console.log($scope.cangku[i]);
                                                                $scope.cangku1.push($scope.cangku[i]);
                                                            }
                                                        }
                                                        
                                                    }
                                                })
                                                .then(function (error) {
                                                    if (error) {
                                                        console.log("error");
                                                    }
                                                });
                                    
                                }
                                })
                            .then(function (error) {
                                if (error) {
                                    console.log("error");
                                }
                            });
                }; 
               //获取仓库列表方法
                $http({
                        url: "../api/Provider?type=1&page=1&rows=9999",
                        method: "GET"
                    })
                            .then(function (res) {
                                if (res.State <= 0) {
                                    alert(res.Msg);
                                } else {
                                    $scope.cangku1 = res.data.Data;
                                }
                            })
                            .then(function (error) {
                                if (error) {
                                    console.log("error");
                                }
                            });
                        
                //提交编辑结果
                $scope.xxx = function () {
                        if ($scope.orderdata.Provider) {
                            $scope.sum();
                            if ($scope.orderdata.Provider==$scope.orderdata.ToDepot) {
                                $scope.genre('盘螺','','');
                            }else{
                               $scope.genre('盘螺','',$scope.orderdata.Provider.Id); 
                            }
                            $scope.isshow = !$scope.isshow;
                        } else {
                            alert("请选择供应商")
                        }
                };
                $scope.change1 = function(){
                    $http({
                        url: "../api/Provider?type=1&page=1&rows=9999",
                        method: "GET"
                    })
                            .then(function (res) {
                                if (res.State <= 0) {
                                    alert(res.Msg);
                                } else {
                                    if($scope.orderdata.Provider && $scope.type != 0){
                                        $scope.orderdata.ToDepot = '';
                                    }
                                }
                            })
                            .then(function (error) {
                                if (error) {
                                    console.log("error");
                                }
                            });
                };
                //计算总价方法
                $scope.sum = function () {
                    $scope.newWeight = 0;
                    $scope.orderdata.Total = 0;
                    $scope.Weight = 0;
                    for (var i = 0; i < $scope.orderdata.Goodses.length; i++) {
                        $scope.Weight = $scope.orderdata.Goodses[i].Count * $scope.orderdata.Goodses[i].Weight;
                        $scope.orderdata.Total += $scope.Weight * $scope.orderdata.Goodses[i].Price;

                        $scope.newWeight += $scope.orderdata.Goodses[i].Count*$scope.orderdata.Goodses[i].Weight;
                    }
                    $scope.orderdata.SourceTotal = $scope.orderdata.Total;
                    $scope.orderdata.Total = $scope.orderdata.Total.toFixed(2);
                    $scope.orderdata.DeliveryPriceTotal = $scope.newWeight*$scope.orderdata.DeliveryPrice;
                };
                // $scope.topname="";
                $scope.genre = function (name,chang,mao) {
//                    if (name != "" && $scope.orderdata.Provider.Supplier.Id != "") {
                        if ($scope.type == 0) {
                        
                            $scope.model1 = $scope.orderdata.Provider.Id;
                        }else if($scope.type == 1){
                            $scope.model1 = "";
                        }
                        if ($scope.orderdata.Provider==$scope.orderdata.ToDepot) {
                                mao="";
                            }
                        console.log($scope.model1);
                        //获取商品列表
                        $http({
                            url: "../api/Provider/DepotCommodity?depotId="+mao+"&name="+name+"&keyStr="+chang+"&page=1&rows=9999",
                            method: "GET"
                        })
                        .then(function (res) {
                            // $scope.DepotCommodity == res.data;
                                    if (res.State <= 0) {
                                        alert(res.Msg);
                                    } else {
                                        $scope.DepotCommodity =[];
                                        // console.log(res.data.Commodity)
                                        for (var i = 0; i < res.data.length; i++) {
                                            $scope.DepotCommodity.push(res.data[i].Commodity);
                                            // console.log(res.data[i].Commodity);
                                        }
                                    }
                                })
                                .then(function (error) {
                                    if (error) {
    
                                    }
                                });
//                    }
                };
                //添加到购物车
                // console.log($scope.findName);
                // console.log($scope.topname)
                $scope.find=function(){
                    $scope.genre($scope.findName,$scope.topname,$scope.orderdata.Provider)
                    
                }
                $scope.add = function (item) {
                    for (var i = 0; i < $scope.orderdata.Goodses.length; i++) {
                        if ($scope.orderdata.Goodses[i].Id == item.Id) {
                            alert("您已添加过此商品，不能重复添加！");
                            return;
                        }
                    }
                    item.isadd = true;
                    item.Count = 1;
                    item.Price = 1;
                    $scope.sum();
                    $scope.orderdata.Goodses.push(item);
                };
                //供货商改变清空购物车
                $("#Provider").change(function () {
                    $scope.orderdata.Goodses = [];
                     $scope.commodity=[];
                    $scope.sum();
                });
                //从购物车删除
                $scope.deduct = function (item) {
                    item.isadd = false;
                    item.Count = 0;
                    $scope.sum();
                    for (var i = 0; i < $scope.orderdata.Goodses.length; i++) {
                        if ($scope.orderdata.Goodses[i].CommodityId == item.CommodityId) {
                            $scope.orderdata.Goodses.splice(i, 1);
                        }
                    }
                };
                //购物车增加数量
                $scope.Countadd = function (item) {
                    item.Count += 1;
                    $scope.sum();
                };
                //购物车减少数量
                $scope.Countdeduct = function (item) {
                    if (item.Count > 0) {
                        item.Count -= 1;
                        $scope.sum();
                    }
                };
                $scope.orderdata = {
                    Goodses: [],
                    Total: 0,
                    DeliveryPrice : 0
                };
                //提交采购单
                $scope.ordersub = function () {
                    $scope.orderdata.TotalWeight = $scope.newWeight;
                    $scope.orderdata.DeliveryPriceTotal = $scope.newWeight*$scope.orderdata.DeliveryPrice;
                    if ($scope.orderdata.Goodses.length < 1) {
                        alert("请添加商品")
                    } else {
                       console.log($scope.orderdata);
                        $http.post('../api/Purchase', $scope.orderdata)
                                .success(function (res) {
                                    if (res.State <= 0) {
                                        alert(res.Msg);
                                    } else {
                                        alert("添加成功");
                                        $state.go('app.Purchasing_MyPurList');
                                    }
                                });
                    }
                };
                //返回上一页
                $scope.abolish = function () {
                    $state.go('app.Purchasing_MyPurList');
                };
                $scope.selectEmp= function(emp) {
                    $scope.orderdata.Provider = emp;
                    $scope.change1();
                    $('#dlg').modal("hide");
                };
                $scope.empty = function(){
                    $scope.orderdata.Provider = "";
                };
                    if($scope.type == 0){
                        //获取钢厂列表
                        $scope.acquire(0);
                    }else if($scope.type == 1){
                         //获取贸易商列表
                         $scope.acquire(2);
                    }
            })
</script>